/*
 * 软件著作权：北京康兰尚德数据科技有限公司
 * 项目名称：康兰数据购物中心分析平台
 *
 * NAME : CommentDaoImpl.java
 *
 * PURPOSE : 用户管理数据库访问
 *
 * AUTHOR : EEDATA/ywb
 *
 *
 * 创建日期: 2013-11-27
 * HISTORY：
 * 变更日期 
 */
package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pojo.EcpPpBrandcomment;
import pojo.EcpPpStorycomment;
import pojo.EcpYhUser;
import util.HibernateUtils;

/**
 * 活动表数据访问层
 * 
 * @author ywb
 */

public class CommentDaoImpl {

    /**
     * 根据新品id查询评价信息
     */
    @SuppressWarnings("unchecked")
    public List<EcpPpBrandcomment> selectNewBrandcomments(String newbrandid) {// 四、getBrandReviewInfoList接口
        Session session = null;
        Transaction ts = null;
        List<EcpPpBrandcomment> list = null;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            Query query = session.createQuery("from EcpPpBrandcomment where braotherid=?");
            query.setParameter(0, Integer.parseInt(newbrandid));
            list = query.list();

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return list;

    }


    /**
     * 根据用户id查询用户名
     */
    public String selectUsers(Integer userid) {// 四、getBrandReviewInfoList接口 //
                                               // 二十、getStoryCommentInfoList接口
        Session session = null;
        Transaction ts = null;
        String name = null;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            EcpYhUser user = (EcpYhUser) session.get(EcpYhUser.class, userid);
            name = user.getUsername();

            if (name.equals("") || name == null)
                name = user.getEmail();

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return name;

    }


    /**
     * 添加新品评论
     */
    public boolean insertNewBrandComment(EcpPpBrandcomment bracom) {// 五、addReview接口
        Session session = null;
        Transaction ts = null;
        boolean row = true;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            session.persist(bracom);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            row = false;
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return row;

    }


    /**
     * 添加故事评论
     */
    public boolean inserStoryComment(EcpPpStorycomment story) {// 十九、addStoryComment接口
        Session session = null;
        Transaction ts = null;
        boolean row = true;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            session.persist(story);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            row = false;
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return row;

    }


    /**
     * 根据故事id查询评价信息
     */
    @SuppressWarnings("unchecked")
    public List<EcpPpStorycomment> selectStorycomments(String stortid) {// 二十、getStoryCommentInfoList接口
        Session session = null;
        Transaction ts = null;
        List<EcpPpStorycomment> list = null;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            Query query = session.createQuery("from EcpPpStorycomment where storyid=?");
            query.setParameter(0, Integer.parseInt(stortid));
            list = query.list();

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return list;

    }
}
